<?php
/** @brief Clase que permite asignar claves a un arreglo solo si fueron registradas antes. 
*/
class u_regarray{
		private $arr=array(); ///< array asocaitivo $arr['key']='value', key debe estar registrada
		private $count=0;    ///< contador de claves registradas
		private $assigned=0; ///< contador de la cantidad de claves asignadas con un valor <>''
		
		private function checkkey($key){
			if( !$this->have($key) )
				throw new Exception('La clave ['.$key.'] no fue registrada');
		}

/**
*/
		function reg($key){
			$this->arr[$key]='';
			$this->count++;
		}

		function ureg($key){
			$this->checkkey($key);
			if($this->arr[$key]!='')
				$this->assigned--;
			unset($this->arr[$key]);
			$this->count--;
		}

		function reg_arr($key_arr){
			foreach($key_arr as $key)
				$this->reg($key);
		}

		function ureg_arr($key_arr){
			foreach($key_arr as $key)
				$this->ureg($key);
		}
		
		function ureg_all(){
			unset($this->arr);
			$this->arr=array();
			$this->count=0;
			$this->assigned=0;
		}
	
		function have($key){
			return isset($this->arr[$key]);
		}

		function assingned($key){
			return $this->arr[$key]<>'';
		}


		function set($key,$val){
			$this->checkkey($key);
			if(!emptystr($val)){
				$this->assigned++;
				if(is_array($this->arr[$key]))
					array_push($this->arr[$key],$val);
				else
					$this->arr[$key]=array($val);
			}
		}

//retorna siempre un array
		function get($key){
			$this->checkkey($key);
			return $this->arr[$key];
		}

//retorna siempre un array, key devuelve el nombre de la clave
		function getn($i,&$key){
			if($i<0 || $i>=$this->count())
				throw new Exception('Indice fuera de rango: ['.$i.']');
			$a = array_keys($this->arr);
			$key=$a[$i];
			return $this->get($key);
		}
		
		function count(){
			return $this->count;
		}
		
		function assigned(){
			return $this->assigned;
		}
}
?>